package com.reider.log4AS3.utils
{
	import com.reider.log4AS3.dataholder.FormatedMsg;
	
	
	/**
	 * @private
	 */
	public class MessageFormatter
	{
		
		public static const OBJPATTERN:String = "{}";
		
		public static function formatString(format:String, args:Array):FormatedMsg
		{	
			var fortmatedText:String = ""
			var curArgs:Array = args.concat()
			var additionalArgs:Array = new Array()
        	var patterns:Array = format.split( OBJPATTERN )
        	var patternLength:int
        	
        	if(patterns.length-1 < 0){
        		patternLength = 0
        		additionalArgs = curArgs
        	}else{
        		patternLength = patterns.length-1
	        	fortmatedText = patterns[0];
				additionalArgs = curArgs.splice(patternLength)
	        	
	        	for(var i:int=0; i<patternLength; i++){
	        		var arg:String = curArgs[i]
	        		if(arg != null){
	        			fortmatedText += curArgs[i]+patterns[i+1];
	        		}else{
	        			fortmatedText += patterns[i+1];
	        		}
	        	}
	        }
        	
        	return new FormatedMsg(fortmatedText, additionalArgs)
		}
		
		public static function replace(str:String, pattern:String, rep:String):String
		{
			var regex:RegExp = new RegExp(pattern, "gim")
			
			return str.replace(regex,rep)
		}
	}
}